#include <iostream>
using namespace std;

#include "client.h"
#include "server.h"

#include <absl/flags/flag.h>
#include <absl/flags/parse.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>

void runClient()
{
    std::string target_str = "localhost:10086";

    TestClient  client(grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
    std::string reply = client.getMessage("Hello, World!");
    std::cout << "Client received: " << reply << std::endl;
}

void runServer()
{
    std::string server_address = "0.0.0.0:10086";
    ServiceImpl service;

    grpc::EnableDefaultHealthCheckService(true);
    grpc::reflection::InitProtoReflectionServerBuilderPlugin();

    grpc::ServerBuilder builder;
    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
    builder.RegisterService(&service);
    std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
    std::cout << "Server listening on " << server_address << std::endl;
    server->Wait();
}

void printUsage(const char *cmd)
{
    printf("Usgae: %s <command>\n\n", cmd);
    printf("commands: \n");
    printf(" client\n");
    printf(" server\n");
}

int main(int argc, char *argv[])
{
    if (argc < 2) {
        printUsage(argv[0]);
        return 0;
    }

    if (strcmp(argv[1], "client") == 0) {
        runClient();
    } else if (strcmp(argv[1], "server") == 0) {
        runServer();
    } else {
        printUsage(argv[0]);
    }
    return 0;
}